package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class SubmitResumeServlet
 */
@WebServlet("/SubmitResumeServlet")
public class SubmitResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SubmitResumeServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//logger.info("previewButton -> " + request.getParameter("previewButton"));
		//logger.info("confirmButton -> " + request.getParameter("submitButton"));
		//logger.info("headingFont -> " + request.getParameter("headingFont"));
		if(request.getParameter("previewButton") != null){
			//logger.info("[1] -> preview Resume");
			showPreview(request, out);
		}else{
			//logger.info("[2] -> confirm Resume");
			storeResume(request);
			showConfirmation(request, out);
		}
	}
	
	private void showPreview(HttpServletRequest request, PrintWriter out){
		String headingFont = request.getParameter("headingFont");
		headingFont = replaceIfMissingOrDefault(headingFont, "");
		
		int headingSize = getSize(request.getParameter("headingSize"), 32);
		
		String bodyFont = request.getParameter("bodyFont");
		bodyFont = replaceIfMissingOrDefault(bodyFont, "");
		
		int bodySize = getSize(request.getParameter("bodySize"), 18);
		
		String fgColor = request.getParameter("fgColor");
		fgColor = replaceIfMissing(fgColor, "black");
		
		String bgColor = request.getParameter("bgColor");
		bgColor = replaceIfMissing(bgColor, "white");
		
		String name= request.getParameter("name");
		name = replaceIfMissing(name, "Lou Zer");
		
		String title = request.getParameter("title");
		title = replaceIfMissing(title, "Loser");
		
		String email = request.getParameter("email");
		email = replaceIfMissing(email, "contact@hot-computer-jobs.com");
		
		String languages = request.getParameter("languages");
		languages = replaceIfMissing(languages, "<em>None</em>");
		//String languageList = makeList(languages);
		
		String skills = request.getParameter("skills");
		skills = replaceIfMissing(skills, "Not many, obviously.");
		
		//
		out.println(Constants.docType + "\n" +
			"<html><head><title>Resume for " + name + "</title>\n" +
			makeStyleSheet(headingFont, headingSize, bodyFont, bodySize, fgColor, bgColor) +
			"\n</head>\n" +
			"<body><center><span class=\"heading1\">" + name + "</span><br/>\n" +
			"<span class=\"heading2\">" + title + "<br/>\n" +
			"<a href=\"mailto:" + email + "\">" + email + "</a></span>\n<br/>" +
			"<span class=\"heading3\">Programming Languages" +
			"</span>\n" + makeList(languages) +"<br/><br/>\n" +
			"<span class=\"heading3\">Skills and Experience" + "</span><br/><br/>\n" +
			skills + "\n</body></html>"
		);
		
		
	}
	
	private String makeStyleSheet(String headingFont,
				int heading1Size,
				String bodyFont,
				int bodySize,
				String fgColor,
				String bgColor
			){
		
		int heading2Size = heading1Size * 7/10;
		int heading3Size = heading1Size * 6/10;
		
		//
		String styleSheet = 
				"<style type=\"text/css\">\n" +
				"<!--\n" +
				".heading1{font-size:"+heading1Size+"px;\n"+
				"font-weight:bold;\nfont-family:" + headingFont + "Arial, Helvetica, sans-serif;\n" +
				"}\n" +
				".heading2{font-size:" + heading2Size + "px;\n" +
				"font-weight:bold;\nfont-family:" + headingFont + "Arial, Helvetica, sans-serif;\n" +
				"}\n" +
				"heading3{font-size:"+ heading3Size + "px;\n" +
				"font-weight:bold;\nfont-family:" + headingFont + "Arial, Helvetica, sans-serif;\n" +
				"}\n" +
				"body{ color:" + fgColor + ";\n" + 
				"background-color: " + bgColor + ";\n" + 
				"font-size: " + bodySize + "px;\n" + 
				"font-family: " + bodyFont + "Times New Roman, Times, serif;\n" + 
				"}\n" + 
				"a:hover{color:read;}\n" + 
				"-->\n" + 
				"</style>"
				
						;
		return styleSheet;
	}
	
	private String replaceIfMissingOrDefault(String value, String defValue){
		
		if(null == value||
			value.trim().isEmpty()||
			value.equals("default")
				){
			return defValue;
		}else{
			return value + ", ";
		}
		
	}
	
	private String replaceIfMissing(String value, String defaultValue){
		if(null == value||
				value.trim().isEmpty()	
					){
				return defaultValue;
			}else{
				return value;
			}
	}
	
	private int getSize(String value, int defaultValue){
		//int retval = defaultValue;
		
		try{
			return Integer.parseInt(value);
		}catch(NumberFormatException e){
			//logger.info("Invalid number format: [" + value + "]");
			return defaultValue;
		}
		
		//return retval;
	}
	
	private String makeList(String list){
		StringBuilder retval = new StringBuilder();
		retval.append("<ul>\n");
		
		//logger.info("Programming Language List -> " + list);
		String[] languages = list.split(",");
		for(String language : languages){
			retval.append("<li>" + language + "</li>");
		}
		
		
		retval.append("</ul>");
		return retval.toString();
	}
	
	private void storeResume(HttpServletRequest request){
		String email = request.getParameter("email");
		putInSpamList(email);
	}
	
	private void showConfirmation(HttpServletRequest request, PrintWriter out){
		String title = "Submission Confirmed.";
		out.println(ServletUtilities.headWithTitle(title) + 
				"<body><h1>" + title + "</h1>Your resume shoud appear online" + 
				"withing\n 24 hours. If it dosen't, try submitting\n again with" +
				"a different email address.\n</body></html>");
	}
	
	private void putInSpamList(String email){
		//do-nothing;
	}
	private static final Logger logger = 
			Logger.getLogger(SubmitResumeServlet.class);

}
